package com.example.coffeeseller_2.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.utils.DensityUtil;

/**
 * Created by WH on 2018/7/17.
 * 咖啡2圆角图片
 */

public class CoffeeTwoRoundImage extends android.support.v7.widget.AppCompatImageView {

    float width, height;
    float radius;
    int conerMode;

    public CoffeeTwoRoundImage(Context context) {
        super(context);

    }

    public CoffeeTwoRoundImage(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CoffeeTwoRoundImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, @Nullable AttributeSet attrs) {
        if (Build.VERSION.SDK_INT < 18) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        TypedArray attrsArray = context.obtainStyledAttributes(attrs, R.styleable.CoffeeTwoRoundImage);

        try {
            conerMode = attrsArray.getInt(R.styleable.CoffeeTwoRoundImage_conerMode, 0);
            radius = attrsArray.getDimension(R.styleable.CoffeeTwoRoundImage_bottom_radius, 0);
        }finally {
            attrsArray.recycle();
        }
//        radius = radius * 4 / 3;//由于输入为20的时候，输出为15，输入为25时，输出为18.75，故此，保证数据与设置的一致
        radius = DensityUtil.dp2px(context, radius);
    }

    @Override

    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        super.onLayout(changed, left, top, right, bottom);

        width = getWidth();

        height = getHeight();

    }


    @Override

    protected void onDraw(Canvas canvas) {

        if (conerMode == 1) { //Bottom
            if (width > radius && height > radius) {

                Path path = new Path();
                path.moveTo(radius, height);
                path.lineTo(width - radius, height);
                path.quadTo(width, height, width, height - radius);
                path.lineTo(width, 0);
                path.lineTo(0, 0);
                path.lineTo(0, height - radius);
                path.quadTo(0, height, radius, height);

                canvas.clipPath(path);

            }
        } else if (conerMode == 0) { //top
            if (width > radius && height > radius) {

                Path path = new Path();
             /*   path.moveTo(radius, 0);
                path.lineTo(width - radius, 0);
                path.quadTo(width, 0, width, radius);
                path.lineTo(width, height);
                path.lineTo(0, height);
                path.lineTo(0, radius);
                path.quadTo(0, 0, radius, 0);*/

                path.moveTo(0, height);
                path.lineTo(width, height);
                path.lineTo(width, radius);
                path.quadTo(width, 0, width - radius, 0);
                path.lineTo(radius, 0);
                path.quadTo(0, 0, radius, 0);
                path.lineTo(0, height);

                canvas.clipPath(path);
            }
        }


        super.onDraw(canvas);

    }
}
